package com.yu.shortlink.admin.controller;

import com.yu.shortlink.admin.common.convention.result.Result;
import com.yu.shortlink.admin.common.convention.result.Results;
import com.yu.shortlink.admin.dto.req.UserLoginReqDTO;
import com.yu.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.yu.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.yu.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.yu.shortlink.admin.dto.resp.UserRespDTO;
import com.yu.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author yushiwang
 * @Date 2024/3/26 22:15
 * @PackageName:com.yu.shortlink.admin.controller
 * @ClassName: UserController
 * @Version 1.0
 */

/**
 * 用户模块
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public UserRespDTO getUserByUsername(@PathVariable("username")String username){
        UserRespDTO userRespDTO = userService.getUserByUsername(username);
        return userRespDTO;
    }

    /**
     * 查询用户名是否存在
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUserName(username));
    }

    /**
     * 注册用户
     * @param userRegisterReqDTO
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO){
        userService.register(userRegisterReqDTO);
        return Results.success();
    }

    /**
     * 修改用户信息
     * @param userUpdateReqDTO
     * @return
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO userUpdateReqDTO){
        userService.update(userUpdateReqDTO);
        return Results.success();
    }

    /**
     * 用户登陆
     * @param userLoginReqDTO
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO){
        UserLoginRespDTO userLoginRespDTO = userService.login(userLoginReqDTO);
        return Results.success(userLoginRespDTO);
    }

    /**
     * 检测用户是否登陆
     * @param username
     * @param token
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/check-token")
    public Result<Boolean> checkLogin(@RequestParam("username")String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 退出登陆
     * @param username
     * @param token
     * @return
     */
    @DeleteMapping("/api/short-link/admin/v1/user/check-logout")
    public Result<Void> logout(@RequestParam("username")String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }

}
